package com.tojabo.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class FileUtil {
	public static String FS = File.separator;
	public static String FPS = File.pathSeparator;

	public static Map<String, List<File>> map = new HashMap<String, List<File>>();
	public static List<File> list = new ArrayList<File>();

	public static void main(String[] args) {

		String pitchNames = "a0,a0m,b0,"
				+ "c1,c1m,d1,d1m,e1,f1,f1m,g1,g1m,a1,a1m,b1,"
				+ "c2,c2m,d2,d2m,e2,f2,f2m,g2,g2m,a2,a2m,b2,"
				+ "c3,c3m,d3,d3m,e3,f3,f3m,g3,g3m,a3,a3m,b3,"
				+ "c4,c4m,d4,d4m,e4,f4,f4m,g4,g4m,a4,a4m,b4,"
				+ "c5,c5m,d5,d5m,e5,f5,f5m,g5,g5m,a5,a5m,b5,"
				+ "c6,c6m,d6,d6m,e6,f6,f6m,g6,g6m,a6,a6m,b6,"
				+ "c7,c7m,d7,d7m,e7,f7,f7m,g7,g7m,a7,a7m,b7," + "c8";

		String filepath = "E:/data_music";

		String[] nameArrays = pitchNames.split(",");

		StringBuffer sbf = new StringBuffer();

		for (String pitch : nameArrays) {
			sbf.append(filepath + "/" + pitch + ".ogg" + "\r\n");
		}

		System.out.println(sbf.toString());
		String pitchfile = "E:/pitchName.txt";
		fileWriter(pitchfile, sbf.toString());

	}

	public static boolean isExists(File f) {
		// File f=new File(filename);
		if (f.exists()) {
			return true;
		} else {
			System.out.println("文件不存在");
		}
		return false;
	}

	public static String inputStream(String fileName) {
		byte[] b = new byte[1024];
		try {
			File f = new File(fileName);
			InputStream in = new FileInputStream(f);
			int count = 0;
			int temp = 0;
			while ((temp = in.read()) != (-1)) {
				b[count++] = (byte) temp;
			}
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new String(b);
	}

	public static String fileReader(String fileName) {
		StringBuffer sb = new StringBuffer();
		try {
			File f = new File(fileName);
			Reader read = new FileReader(f);
			int temp = 0;
			while ((temp = read.read()) != (-1)) {
				sb.append((char) temp);
			}
			read.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static String inputStreamReader(String fileName, String charset) {
		StringBuffer sb = new StringBuffer();
		try {
			File f = new File(fileName);
			Reader read = new InputStreamReader(new FileInputStream(f), charset);
			int temp = 0;
			while ((temp = read.read()) != (-1)) {
				sb.append((char) temp);
			}
			read.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static List<String> readLines(String fileName) {
		List<String> strlist = new ArrayList<String>();
		try {
			FileInputStream f = new FileInputStream(fileName);// "E:/music.txt"
			// DataInputStream dr = new DataInputStream(f);
			BufferedReader dr = new BufferedReader(new InputStreamReader(f));
			String line = null;
			while ((line = dr.readLine()) != null) {
				if (StringUtil.isNotBlank(line)) {
					// System.out.println(line);
					strlist.add(line);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return strlist;
	}

	public static List<File> printAllFile(File file) {
		if (file.isDirectory()) {
			File[] fileArray = file.listFiles();
			for (File zfile : fileArray) {
				printAllFile(zfile);
			}
		} else {
			// System.out.println(file);
			list.add(file);
		}

		return list;
	}

	public static void putAllFileInMap(File file) {
		if (file.isDirectory()) {
			map.put(file.toString(), new ArrayList<File>());
			File[] fileArray = file.listFiles();
			for (File zfile : fileArray) {
				putAllFileInMap(zfile);
			}
		} else {
			List<File> list = map.get(file.getParent());
			list.add(file);
			// System.out.println(file.getParent()+"=:="+file.getName());
		}
	}

	public static void BianLi(Map<String, List<File>> map) {
		for (Entry<String, List<File>> entry : map.entrySet()) {
			for (File file : entry.getValue()) {
				System.out.println(entry.getKey() + "," + file.getName());
			}
		}
	}

	public static boolean copyFile(File from, File to) {
		if (!from.exists()) {
			System.out.println("被复制的文件不存在");
		}
		try {
			InputStream input = new FileInputStream(from);
			OutputStream output = new FileOutputStream(to);
			if ((input != null) && (output != null)) {
				int temp = 0;
				while ((temp = input.read()) != (-1)) {
					output.write(temp);
				}
			}
			input.close();
			output.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public static void outStream(String filename, String content,
			boolean isAppend) {
		try {
			File f = new File(filename);
			OutputStream out = new FileOutputStream(f, isAppend);
			byte[] b = content.getBytes();
			int len = b.length;
			for (int i = 0; i < len; i++) {
				out.write(b[i]);
			}
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void fileWriter(String fileName, String content) {
		try {
			File f = new File(fileName);
			Writer out = new FileWriter(f);
			out.write(content);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
